package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.Regions;
import cn.exrick.xboot.modules.transport.mapper.CompanyWarehousesMapper;
import cn.exrick.xboot.modules.transport.vo.RegionsVo;
import cn.exrick.xboot.modules.transport.param.QueryRegionsParam;
import cn.exrick.xboot.modules.transport.param.EditRegionsParam;
import cn.exrick.xboot.modules.transport.service.RegionsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "地区信息管理接口")
@RequestMapping("/xboot/regions")
@Transactional
public class RegionsController {

    @Autowired
    private RegionsService regionsService;
    @Autowired
    private CompanyWarehousesMapper companyWarehousesMapper;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<RegionsVo> get(@PathVariable Long id) {

        RegionsVo regionsvo = BeanConverterUtil.convertBean(regionsService.getById(id), RegionsVo.class);
        return new ResultUtil<RegionsVo>().setData(regionsvo);
    }


    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<RegionsVo> saveOrUpdate(@RequestBody EditRegionsParam editRegionsParam) {
        if (editRegionsParam.getId() != null) {
            //同步更新仓点名称
        }
        if (regionsService.saveOrUpdateRegions(editRegionsParam)) {
            return new ResultUtil<RegionsVo>().setData(BeanConverterUtil.convertBean(editRegionsParam, RegionsVo.class));
        }
        return new ResultUtil<RegionsVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            regionsService.removeRegionsById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<RegionsVo>> getByCondition(QueryRegionsParam param,
                                                   PageVo page){
        return new ResultUtil<IPage<RegionsVo>>().setData(regionsService.getByCondition(PageUtil.initMpPage(page),param));
    }
}
